package com.business.etl.flow.log;

import cn.hutool.log.level.Level;
import com.business.etl.flow.mq.LogProducer;
import com.common.log.LogFlowInfo;
import com.common.log.Logger;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * 描述：
 * 日志处理实体 最好一个流程
 * @author xianggj
 * @Date 2021/10/25 18:08
 **/
@Slf4j
@Data
public class MQLogger implements Logger, Serializable {

    private static final long serialVersionUID = -1L;

    private LogProducer logProducer;

    /**
     * 必须参数
     * @param logProducer
     * @param flowId
     */
    public MQLogger(LogProducer logProducer, String flowId) {
        super();
        this.logProducer = logProducer;
        this.flowId = flowId;
    }

    /**
     * 流程唯一标识
     */
    private String flowId;
    /**
     * 流程唯一标识
     */
    private String flowName;

    /**
     * 节点唯一标识
     */
    private String nodeId;

    /**
     * 节点名称
     */
    private String nodeName;

    /**
     * 发送间隔
     */
    private static final long INTERVAL = 500L;

    @Override
    public void info(String msg) {
        log.info(msg);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.INFO);
        logProducer.addLog(logFlowInfo, flowId);
    }

    @Override
    public void error(String msg) {
        log.error(msg);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.ERROR);
        logProducer.addLog(logFlowInfo, flowId);
    }

    private LogFlowInfo getLogFlowInfo(String msg, Level error) {
        LogFlowInfo logFlowInfo = new LogFlowInfo();
        logFlowInfo.setId(System.nanoTime());
        logFlowInfo.setFlowId(flowId);
        logFlowInfo.setFlowName(flowName);
        logFlowInfo.setNodeId(nodeId);
        logFlowInfo.setNodeName(nodeName);
        logFlowInfo.setMessage(msg);
        logFlowInfo.setLevel(error);
        logFlowInfo.setTime(System.currentTimeMillis());
        return logFlowInfo;
    }

    @Override
    public void warn(String msg) {
        log.warn(msg);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.WARN);
        logProducer.addLog(logFlowInfo, flowId);
    }

    @Override
    public void debug(String msg) {
        log.debug(msg);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.DEBUG);
        logProducer.addLog(logFlowInfo, flowId);
    }

    @Override
    public void fatal(String msg) {
        log.warn(msg);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.FATAL);
        logProducer.addLog(logFlowInfo, flowId);
    }

    @Override
    public void release() {
        //释放资源
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void info(String format, Object... args) {
        log.info(format, args);
        String msg = String.format(format, args);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.INFO);
        logProducer.addLog(logFlowInfo, flowId);
    }

    @Override
    public void error(String format, Object... args) {
        log.error(format, args);
        String msg = String.format(format, args);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.ERROR);
        logProducer.addLog(logFlowInfo, flowId);
    }

    @Override
    public void debug(String format, Object... args) {
        log.debug(format, args);
        String msg = String.format(format, args);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.DEBUG);
        logProducer.addLog(logFlowInfo, flowId);
    }

    @Override
    public void fatal(String format, Object... args) {
        String msg = String.format(format, args);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.FATAL);
        logProducer.addLog(logFlowInfo, flowId);
    }

    @Override
    public void warn(String format, Object... args) {
        log.warn(format, args);
        String msg = String.format(format, args);
        LogFlowInfo logFlowInfo = getLogFlowInfo(msg, Level.WARN);
        logProducer.addLog(logFlowInfo, flowId);
    }
}
